package com.waterworld.watch.common.activity;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.waterworld.watch.R;
import com.waterworld.watch.common.customview.dialog.LoadingDialog;
import com.waterworld.watch.common.util.AppManager;

public class BaseActivity extends AppCompatActivity {

    private final String TAG = BaseActivity.class.getSimpleName();
    private InputMethodManager manager;//输入管理类

    protected LoadingDialog mLoadingDialog;
    private LoadingDialog.Speed speed = LoadingDialog.Speed.SPEED_TWO;
    private int repeatTime = 0;
    private int color = Color.argb(255, 1, 162, 157);

    PointF downPoint = new PointF();
    PointF currentPoint = new PointF();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.addActivity(this);
        manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        setTranslucentStatus(true);
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);//calculateStatusColor(Color.WHITE, (int) alphaValue)
        }
    }


    public void setViewSize(View view, int width, int height) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = width;
        params.height = height;
        view.setLayoutParams(params);
    }

//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        super.onTouchEvent(event);
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
//                    manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//                }
//                break;
//        }
//        return false;
//    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        currentPoint.x = ev.getX();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downPoint.x = ev.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                if (currentPoint.x - downPoint.x > 500) {
                    finish();
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.finishActivity(this);
    }

    public void showDialog(String loading,String loadSuccess,String loadFail){
        if(mLoadingDialog == null){
            mLoadingDialog = new LoadingDialog(this);
        }
        if(loading != null && !loading.equals("")){
            mLoadingDialog.setLoadingText(loading);
        }else {
            mLoadingDialog.setLoadingText(getString(R.string.loading));
        }

        if(loadSuccess != null && !loadSuccess.equals("")){
            mLoadingDialog.setSuccessText(loadSuccess);
        }else {
            mLoadingDialog.setSuccessText(getString(R.string.load_success));
        }

        if(loadFail != null && !loadFail.equals("")){
            mLoadingDialog.setFailedText(loadFail);
        }else {
            mLoadingDialog.setFailedText(getString(R.string.load_fail));
        }
        mLoadingDialog.setLoadStyle(LoadingDialog.STYLE_RING); //转圈圈样式
        mLoadingDialog.setLoadSpeed(speed);
        mLoadingDialog.setRepeatCount(repeatTime);
        mLoadingDialog.setDrawColor(color);
        mLoadingDialog.setTextColor(Color.WHITE);
        mLoadingDialog.show();
    }

    public void closeDialog(){
        if(mLoadingDialog != null){
            mLoadingDialog.close();
            mLoadingDialog = null;
        }
    }
}
